unit ModeloBusca;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids,
  Vcl.ExtCtrls, Data.FMTBcd, Data.DB, Datasnap.Provider, Datasnap.DBClient,
  Data.SqlExpr, Vcl.Buttons, Vcl.Menus, Clipbrd;

type
  TfrmModeloBusca = class(TForm)
    pnlInferior: TPanel;
    DBGridBusca: TDBGrid;
    pnlSuperior: TPanel;
    lblTituloForm: TLabel;
    qrBusca: TSQLQuery;
    cdsBusca: TClientDataSet;
    dspBusca: TDataSetProvider;
    dsBusca: TDataSource;
    Label1: TLabel;
    edtPesquisaAvancada: TEdit;
    btnCancelar: TBitBtn;
    popupRemover: TPopupMenu;
    Remover1: TMenuItem;
    qrRemover: TSQLQuery;
    Copiar2: TMenuItem;
    N1: TMenuItem;
    lblDica: TLabel;
    procedure edtPesquisaAvancadaKeyPress(Sender: TObject; var Key: Char);
    procedure btnCancelarClick(Sender: TObject);
    procedure Copiar2Click(Sender: TObject);
    procedure DBGridBuscaDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure popupRemoverPopup(Sender: TObject);
  private
    procedure habilitarPopupMenuLicoes(habilitado: Boolean);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmModeloBusca: TfrmModeloBusca;

implementation

uses
    Principal;

{$R *.dfm}

procedure TfrmModeloBusca.btnCancelarClick(Sender: TObject);
begin
    Close;
end;

procedure TfrmModeloBusca.Copiar2Click(Sender: TObject);
begin
    Clipboard.Open;
    Clipboard.AsText := DBGridBusca.Fields[0].Text; //DBGridBusca.SelectedField.AsString;
    Clipboard.Close;
end;

procedure TfrmModeloBusca.DBGridBuscaDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
    if odd(cdsBusca.RecNo) then
        DBGridBusca.Canvas.Brush.Color:= clWhite
    else
        DBGridBusca.Canvas.Brush.Color:= clSilver;

    TDbGrid(Sender).Canvas.font.Color:= clBlack;

    if gdSelected in State then
    with (Sender as TDBGrid).Canvas do
    begin
        Brush.Color := clMenuHighlight;
        FillRect(Rect);
        Font.Style := [fsbold];
        Font.Color := clWhite
    end;

    TDbGrid(Sender).DefaultDrawDataCell(Rect, TDbGrid(Sender).columns[datacol].field, State);
end;

procedure TfrmModeloBusca.edtPesquisaAvancadaKeyPress(Sender: TObject;
  var Key: Char);
begin
    cdsBusca.Locate('descricao', Trim(edtPesquisaAvancada.Text), [loCaseInsensitive, loPartialKey]);
end;

procedure TfrmModeloBusca.popupRemoverPopup(Sender: TObject);
begin
    if cdsBusca.Active then
    begin
        if cdsBusca.RecordCount < 1 then
            habilitarPopupMenuLicoes(false)
        else
            habilitarPopupMenuLicoes(true);
    end
    else
        habilitarPopupMenuLicoes(false);
end;

procedure TfrmModeloBusca.habilitarPopupMenuLicoes(habilitado : Boolean);
begin
    popupRemover.Items[0].Enabled := habilitado;
    popupRemover.Items[2].Enabled := habilitado;
end;

end.
